/**
 * 
 */
package com.antilia.infomaresme.pages.edit;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.odlabs.wiquery.ui.tabs.Tabs;

import com.antilia.infomaresme.InfomaresmeSession;
import com.antilia.infomaresme.data.dao.interfaces.IItemsDao;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.form.ShinyForm;
import com.antilia.infomaresme.pages.myitems.ListItemsPage;
import com.antilia.infomaresme.provider.UserLdm;
import com.google.inject.Inject;

/**
 * @author Ernesto
 *
 */
public class EditItemPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@Inject
	private IItemsDao iItemsDao;
		
	private LongDescriptionPanel description;

	private Form<Item> form;
	
	public EditItemPanel(String id, final Item item) {
		this(id, Model.of(item));
	}
	
	/**
	 * @param id
	 */
	public EditItemPanel(String id, final IModel<Item> model) {
		super(id, model);
		
		FeedbackPanel feedback = new FeedbackPanel("feedback") {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible()
			{
				return anyMessage();
			}
		};
		add(feedback);
		
		if(model.getObject().getId()== null) {
			model.getObject().setTitle("[Edit me]");
			model.getObject().setAuthor(InfomaresmeSession.getSession().getUser());
			iItemsDao.save(model.getObject());
		}
		
			
		form = new ShinyForm<Item>("form", new CompoundPropertyModel<Item>(model));
		Tabs tabs = new Tabs("tabs");
		
		form.add(tabs);
		
		tabs.add(new PropertiesPanel("properties", model));
		description = new LongDescriptionPanel("description", model);
		tabs.add(description);
		
		tabs.add(new ImagesPanel("images", model));
		
		SubmitLink link = new SubmitLink("link") {
			private static final long serialVersionUID = 1L;
			
			@Override
			protected String getTriggerJavaScript() {
				StringBuilder sb1 = new StringBuilder(100);
				sb1.append("document.getElementById('");
				sb1.append(description.getDescription().getMarkupId());
				sb1.append("').value=CKEDITOR.instances.");
				sb1.append(description.getDescription().getMarkupId());
				sb1.append(".getData();");			
				sb1.append(super.getTriggerJavaScript());
				return sb1.toString();
			}
			
			@Override
			public void onSubmit() {
				if((model.getObject().getCategoryPath()== null)) {
					form.error(getString("error.selectCategory"));
					return;
				}
				
				if((model.getObject().getGeographicPath()== null)) {
					form.error(getString("error.selectLocation"));
					return;
				}
				
				iItemsDao.save(model.getObject());
				setResponsePage(ListItemsPage.class);
			}

		};		
		form.add(link);
		
		AjaxLink<Void> cancel = new AjaxLink<Void>("cancel") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				setResponsePage(new ListItemsPage(new UserLdm(InfomaresmeSession.getSession().getUserId())));	
			}
		};
		form.add(cancel);
		add(form);
	}

	public IItemsDao getiItemsDao() {
		return iItemsDao;
	}

	public void setiItemsDao(IItemsDao iItemsDao) {
		this.iItemsDao = iItemsDao;
	}

}
